package com.iwhalecloud.bss.kite.cucc.order.accept.constructor.inst;

import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferProdInstRel;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.ProdInst;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferProdRel;
import com.iwhalecloud.bss.kite.dataservice.mapper.inst.OfferProdInstRelMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.OfferProdRelMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.graph.impl.RelEdge;
import com.iwhalecloud.bss.kite.manager.util.RelTypeUtils;
import com.iwhalecloud.bss.kite.order.client.dto.InstContainer;
import com.iwhalecloud.bss.kite.order.client.dto.OrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataPublic;
import com.ztesoft.bss.common.util.SpringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 销售品产品实例关系构造器
 */
@Component
public class OfferProdInstRelConstructor extends RelConstructor<OfferProdInstRel> {

    @Override
    public String getTableName() {
        return TableConsts.OfferProdInstRel.getTableName();
    }

    @Override
    public OfferProdInstRel build(OrderRequest orderRequest, RelEdge relEdge, Map<String, Object> params) {
        if (!ObjectUtils.allNotNull(orderRequest, relEdge)) {
            return null;
        }

        if (Objects.equals(InstType.PROD_INST, relEdge.getSourceInstType())) {
            // 接入产品实例和功能产品实例的关系转换成基础销售品实例和功能产品实例的关系
            ProdInst prodInst = InstContainer.ofRequest(orderRequest).getProdInst(relEdge.getSourceInstId());
            if (Objects.nonNull(prodInst)) {
                RelEdge newRelEdge = KiteBeanUtils.transform(relEdge, RelEdge.class);
                newRelEdge.setSourceInstId(prodInst.getCompInstId());
                newRelEdge.setSourceInstType(InstType.BASE_OFFER_INST);
                relEdge = newRelEdge;
            }
        }

        return super.build(orderRequest, relEdge, params);
    }

    @Override
    public OfferProdInstRel newInst(OrderRequest orderRequest, RelEdge relEdge, String offerProdInstRelId) {
        if (!ObjectUtils.allNotNull(orderRequest, relEdge)) {
            return null;
        }

        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(orderRequest.getCustId(),
            relEdge.getSourceInstId());

        String relType = RelTypeUtils.getRelTypeCode(relEdge);
        String roleId = relEdge.getRoleId();
        String detailId = relEdge.getDetailId();
        String compInstId = Optional.ofNullable(offerInst).map(KiteOfferInst::getOfferInstId).orElse(null);

        if (Objects.nonNull(offerInst) && StringUtils.isAnyBlank(roleId, detailId)) {
            KiteInst zInst = querySceneInstService.getKiteInst(orderRequest.getCustId(), relEdge.getTargetInstType(),
                relEdge.getTargetInstId());

            OfferProdRel offerProdRel = getOfferProdRel(offerInst.getOfferId(), zInst.getSpecId(), relType);
            if (StringUtils.isBlank(roleId)) {
                roleId = Optional.ofNullable(offerProdRel).map(OfferProdRel::getRoleId).orElse("-1");
            }

            if (StringUtils.isBlank(detailId)) {
                detailId = Optional.ofNullable(offerProdRel).map(OfferProdRel::getOfferProdRelId).orElse("-1");
            }
        }

        if (Objects.equals(InstType.ADDITIONAL_OFFER_INST, relEdge.getSourceInstType())) {
            compInstId = querySceneInstService.getAdditionalOfferInstCompInstId(orderRequest.getCustId(),
                relEdge.getSourceInstId());
        }
        else if (Objects.equals(InstType.BASE_OFFER_INST, relEdge.getSourceInstType())) {
            compInstId = relEdge.getSourceInstId();
        }

        Date now = new Date();
        OfferProdInstRel offerProdInstRel = new OfferProdInstRel();
        if (StringUtils.isNotBlank(offerProdInstRelId)) {
            offerProdInstRel.setOfferProdInstRelId(offerProdInstRelId);
        }
        else {
            offerProdInstRel.setOfferProdInstRelId(SeqUtils.getNext(TableConsts.OfferProdInstRel));
        }
        offerProdInstRel.setOwnerCustId(orderRequest.getCustId());
        offerProdInstRel.setActionType(ActionType.A.name());
        offerProdInstRel.setOfferInstId(relEdge.getSourceInstId());
        offerProdInstRel.setProdInstId(relEdge.getTargetInstId());
        offerProdInstRel.setRelType(relType);
        offerProdInstRel.setEffDate(now);
        offerProdInstRel.setExpDate(AcceptUtils.getDefaultExpDate());
        offerProdInstRel.setRoleId(roleId);
        offerProdInstRel.setStatusCd(CoreDataPublic.PUB_C_0001_1000);
        offerProdInstRel.setCreateStaff(orderRequest.getStaffId());
        offerProdInstRel.setUpdateStaff(orderRequest.getStaffId());
        offerProdInstRel.setCreateDate(now);
        offerProdInstRel.setUpdateDate(now);
        offerProdInstRel.setStatusDate(now);
        offerProdInstRel.setLastOrderItemId("-1");
        offerProdInstRel.setDetailId(detailId);
        offerProdInstRel.setLanId(orderRequest.getLanId());
        offerProdInstRel.setCompInstId(compInstId);

        return offerProdInstRel;
    }

    @Override
    public OfferProdInstRel getInstRel(String offerInstId, String prodInstId, String relType, String custId) {
        Example example = new Example(OfferProdInstRel.class);
        example.createCriteria().andEqualTo("offerInstId", offerInstId).andEqualTo("prodInstId", prodInstId)
            .andEqualTo("relType", relType).andEqualTo("ownerCustId", custId);
        OfferProdInstRelMapper mapper = SpringUtil.getBean(OfferProdInstRelMapper.class);
        List<OfferProdInstRel> offerProdInstRels = mapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(offerProdInstRels)) {
            OfferProdInstRel offerProdInstRel = offerProdInstRels.get(0);
            if (Objects.equals(ActionType.A.name(), offerProdInstRel.getActionType())) {
                offerProdInstRel.setNewActionType(ActionType.K.name());
            }
            return offerProdInstRel;
        }

        return null;
    }

    /**
     * 获取销售品产品关系规格数据
     * @param offerId
     * @param prodId
     * @param relType
     * @return
     */
    private OfferProdRel getOfferProdRel(String offerId, String prodId, String relType) {
        Example example = new Example(OfferProdRel.class);
        example.createCriteria().andEqualTo("offerId", offerId).andEqualTo("prodId", prodId).andEqualTo("relType",
            relType);
        OfferProdRelMapper mapper = SpringUtil.getBean(OfferProdRelMapper.class);
        List<OfferProdRel> offerProdRels = mapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(offerProdRels)) {
            return offerProdRels.get(0);
        }

        return null;
    }
}
